//
//  PBZoomRotationView.m
//  缩小轮播
//
//  Created by Playboy on 2017/7/26.
//  Copyright © 2017年 tmkj. All rights reserved.
//

#import "PBZoomRotationView.h"
#import "MyCardView.h"
#import "PageControl.h"

#define kScreenWidth            [[UIScreen mainScreen] bounds].size.width
#define kScreenHeight           [[UIScreen mainScreen] bounds].size.height

@interface PBZoomRotationView ()<iCarouselDelegate,iCarouselDataSource>

@property (strong, nonatomic) iCarousel         *myICarousel;
@property (strong, nonatomic) imageClickBlock   clickBlock;
@property (strong, nonatomic) NSTimer           *timer;
//@property (strong, nonatomic) UIPageControl     *pageControl;

@property (nonatomic, strong) PageControl        *pageControl;

@end


@implementation PBZoomRotationView

+(instancetype)direcWithtFrame:(CGRect)frame
                      ImageArr:(NSArray *)imageNameArray
            AndImageClickBlock:(imageClickBlock)clickBlock {
    return [[PBZoomRotationView alloc]initWithtFrame:frame
                                       ImageArr:imageNameArray
                             AndImageClickBlock:clickBlock];
}

#pragma mark 默认初始化方法
-(instancetype)initWithtFrame:(CGRect)frame
                     ImageArr:(NSArray *)imageNameArray
           AndImageClickBlock:(imageClickBlock)clickBlock {
    if(self=[self initWithFrame:frame]) {
        self.imageArray = imageNameArray;
        _myICarousel = [[iCarousel alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        _myICarousel.delegate = self;
        _myICarousel.dataSource = self;
        [self addSubview:_myICarousel];
        _myICarousel.type = iCarouselTypeCustom;
        _myICarousel.pagingEnabled=YES;
        [_myICarousel scrollToItemAtIndex:0 animated:YES];
        //设置图片点击的Block
        self.clickBlock=clickBlock;
        self.timer = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
        [[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSDefaultRunLoopMode];
        [self.timer setFireDate:[NSDate dateWithTimeIntervalSinceNow:5]];
        [self addSubview:self.pageControl];
    }
    return self;
}

//- (UIPageControl *)pageControl {
//    if (!_pageControl) {
//        _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, self.frame.size.height-30, kScreenWidth, 20)];
//        _pageControl.numberOfPages = self.imageArray.count;
//        _pageControl.enabled = NO;
//        _pageControl.currentPage = 0;
//        _pageControl.hidesForSinglePage = YES;
//        _pageControl.pageIndicatorTintColor = [UIColor whiteColor];
//        _pageControl.currentPageIndicatorTintColor = [UIColor tm_mainColor];
//    }
//    return _pageControl;
//}

- (PageControl *)pageControl {
    if (!_pageControl) {
        _pageControl = [[PageControl alloc] initWithFrame:CGRectMake((self.frame.size.width-100)/2+25, 185, 100, 20) andDotCount:self.imageArray.count];
        _pageControl.backgroundColor = [UIColor clearColor];
        [_pageControl setSelectedIndex:0];
    }
    return _pageControl;
}

-(void)carouselDidEndScrollingAnimation:(iCarousel *)carousel {
    _iCarouselindex=carousel.currentItemIndex;
//    self.pageControl.currentPage = _iCarouselindex;
    [self.pageControl setSelectedIndex:_iCarouselindex];
}

#pragma mark - <iCarouselDataSource, iCarouselDelegate>
- (NSInteger)numberOfItemsInCarousel:(__unused iCarousel *)carousel {
    return self.imageArray.count;
}

- (UIView *)carousel:(__unused iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view {
    MyCardView *cardView = nil;
    if (view == nil) {
        cardView= [[MyCardView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth*0.85, 170)];
        view=cardView;
        view.contentMode = UIViewContentModeCenter;
        [cardView refreshWithImageArray:self.imageArray andIndex:index];
    }
    return cardView;
}
- (CGFloat)carousel:(iCarousel *)carousel valueForOption:(iCarouselOption)option withDefault:(CGFloat)value {

    switch (option) {
        case iCarouselOptionWrap: {
            return YES;
        }
        case iCarouselOptionSpacing: {
            return value * 1.1f;
        }
        case iCarouselOptionFadeMax:
        case iCarouselOptionShowBackfaces:
        case iCarouselOptionRadius:
        case iCarouselOptionAngle:
        case iCarouselOptionArc:
        case iCarouselOptionTilt:
        case iCarouselOptionCount:
        case iCarouselOptionFadeMin:
        case iCarouselOptionFadeMinAlpha:
        case iCarouselOptionFadeRange:
        case iCarouselOptionOffsetMultiplier:
        case iCarouselOptionVisibleItems: {
            return value;
        }
    }
}

- (CATransform3D)carousel:(iCarousel *)carousel itemTransformForOffset:(CGFloat)offset baseTransform:(CATransform3D)transform {
    static CGFloat max_sacle = 1.0f;
    static CGFloat min_scale = 0.8f;
    if (offset <= 1 && offset >= -1){
        float tempScale = offset < 0 ? 1 + offset : 1 - offset;
        float slope = (max_sacle-min_scale) / 1;
        CGFloat scale = min_scale+slope*tempScale;
        transform = CATransform3DScale(transform, scale, scale, 1);
    }else{
        transform = CATransform3DScale(transform, min_scale, min_scale, 1);
    }
    return CATransform3DTranslate(transform, offset * self.myICarousel.itemWidth * 1.2, 0.0, 0.0);
    
}
-(void)carousel:(iCarousel *)carousel didSelectItemAtIndex:(NSInteger)index{
    if (self.clickBlock) {
        self.clickBlock(index);
    }
}

#pragma mark - timer
- (void)timerAction {
    // 获取当前页
    NSInteger index = self.myICarousel.scrollOffset;
    
    if (self.imageArray.count>index+1) {
        [_myICarousel scrollToItemAtIndex:1+index animated:YES];
    }else {
        [_myICarousel scrollToItemAtIndex:0 animated:YES];
    }
//    self.pageControl.currentPage = self.myICarousel.scrollOffset;
    [self.pageControl setSelectedIndex:self.myICarousel.scrollOffset];
}





@end
